import { defineStore } from "pinia";
import { login } from "@/api";
export interface UserState {
  username: string;
  isLoggedIn: boolean;
}

export const useUserStore = defineStore("user", {
  state: (): UserState => ({
    username: "",
    isLoggedIn: false,
  }),

  getters: {
    getUserName: (state) => state.username,
    getLoginStatus: (state) => state.isLoggedIn,
  },

  actions: {
    async login(username: string, password: string) {
      // 这里可以添加实际的登录逻辑，如API调用
      // 调用登录接口
      const response = await login(username, password);
      if (response.data.code === 200) {
        this.username = username;
        this.isLoggedIn = true;
        return true;
      }
      return false;
    },

    logout() {
      this.username = "";
      this.isLoggedIn = false;
    },
  },
});
